public class Solution788 {
    public int rotatedDigits(int n) {
        int[] f=new int[10];
        f[0]=0;
        f[1]=1;
        f[2]=5;
        f[5]=2;
        f[6]=9;
        f[8]=8;
        f[9]=6;
        int ans=0;
        for (int i=1;i<=n;i++){
            String target=String.valueOf(i);
            StringBuilder stringBuilder=new StringBuilder();
            int flag=-1;
            for (char c : target.toCharArray()) {
                if (c=='3' || c=='4' || c=='7'){
                    flag=1;
                    break;
                }
                stringBuilder.append(f[c-'0']);
            }
            if (flag==-1){
                if (Integer.parseInt(stringBuilder.toString())!=i){
                    ans++;
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution788().rotatedDigits(10));
    }
}
